package com.guigu.serviceconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceconfig.entity.TbFreightTemplate;
import com.guigu.serviceconfig.mapper.TbFreightTemplateMapper;
import com.guigu.serviceconfig.service.ITbFreightTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@Service
public class TbFreightTemplateServiceImpl extends ServiceImpl<TbFreightTemplateMapper, TbFreightTemplate> implements ITbFreightTemplateService {
    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbFreightTemplate> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbFreightTemplate> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,wrapper);
        if (pageAlbum.getRecords() == null && pageAlbum.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("albumRows",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbFreightTemplate> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbFreightTemplate> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null&&!"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name")))
                wrapper.like("name","%"+searchMap.get("name")+"%");

        }
        return wrapper;
    }
}
